﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace FileNameConverter
{
    public class File
    {
        public int IDkey;           //每個檔案的身份
        public string Filename;     //檔案名稱
        public uint Page;            //頁碼
        public string Extension;    //副檔名
        public File(int idkey,string filename, uint page, string extension)
        {
            IDkey = idkey;
            Filename = filename;
            Page = page;
            Extension = extension;
        }
    }

    /*宣告處理File的委派物件*/
    public delegate void ProcessFileDelegate(File file, string filename, uint page);

    /*用來管理File的物件*/
    public class Files
    {
        ArrayList FilesArray = new ArrayList();
        /*加入*/
        public void AddFile(int idkey, string filename, uint page, string extension)
        {
            FilesArray.Add(new File(idkey, filename, page, extension));
        }
        /*移除全部*/
        public void RemoveAll()
        {
            FilesArray.Clear();
        }
        /*檔案數量*/
        public int FilesCount()
        {
            return FilesArray.Count;
        }
        /*複製到另一個陣列*/
        public void CopyTo(Files files)
        {
            foreach (File fileA in FilesArray)
            {
                files.AddFile(fileA.IDkey, fileA.Filename, fileA.Page, fileA.Extension);
            }
        }
        /*針對特定idkey處理檔案*/
        public void ProcessFile(ProcessFileDelegate processFile,int idkey, string filename, uint page)
        {
            foreach (File file in FilesArray)
            {
                if (file.IDkey == idkey)
                    processFile(file, filename, page);
            }
        }
        /*回全完整名稱，包含檔名、頁碼和副檔名*/
        public string[] getFullName(string directoryPath, int pageLength)
        {
            string[] fullfilenames = new string[FilesArray.Count];
            string pageFormat = "D"+pageLength.ToString();
            int index = 0;
            foreach(File file in FilesArray )
            {
                fullfilenames[index++] = directoryPath + file.Filename + file.Page.ToString(pageFormat) + file.Extension;
            }
            return fullfilenames;
        }
    }
}
